﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WorkStation.UI.Controls
{
    public class MotionLimitRectangle
    {
        private List<MotionShape> _shapes; 
        private RectangleMotion _limitRect;
        public string Description { get; set; }
        private double _xLowPosition;
        public double XLowPosition
        {
            get { return _xLowPosition; }
            set { _xLowPosition = value; }
        }
        private double _xHighPosition;
        public double XHighPosition
        {
            get { return _xHighPosition; }
            set { _xHighPosition = value; }
        }
        private double _yLowPosition;
        public double YLowPosition
        {
            get { return _yLowPosition; }
            set { _yLowPosition = value; }
        }
        private double _yHighPosition;
        public double YHighPosition
        {
            get { return _yHighPosition; }
            set { _yHighPosition = value; }
        }

        public MotionLimitRectangle(string description, List<MotionShape> shapes)
        {
            this.Description = description;
            this._shapes = shapes;
        }
        public void Update(MotionCoordinate xCrd, MotionCoordinate yCrd, MotionCoordinate rCrd, int totalWidth, int totalHeight, int marginLeft, int marginUp)
        {
            _shapes.RemoveAll(s => s.Description == Description);
            double x1 = marginLeft;
            double x2 = marginLeft;
            double y1 = marginUp;
            double y2 = marginUp;
            double r = 0;
            double r2 = 0;
            bool getXLow = false;
            bool getXHigh = false;
            bool getYLow = false;
            bool getYHigh = false;
            bool getR = false;
            if (xCrd != null)
                getXLow = xCrd.GetBoardPosition(_xLowPosition, totalWidth, totalHeight, marginLeft, marginUp, xCrd.Direction, out x1, out r2);
            if (xCrd != null)
                getXHigh = xCrd.GetBoardPosition(_xHighPosition, totalWidth, totalHeight, marginLeft, marginUp, xCrd.Direction, out x2, out r2);
            if (yCrd != null)
                getYLow = yCrd.GetBoardPosition(_yLowPosition, totalWidth, totalHeight, marginLeft, marginUp, yCrd.Direction, out y1, out r2);
            if (yCrd != null)
                getYHigh = yCrd.GetBoardPosition(_yHighPosition, totalWidth, totalHeight, marginLeft, marginUp, yCrd.Direction, out y2, out r2);
            //if (rCrd != null)
            //    getR = rCrd.GetBoardPosition(_rPosition, totalWidth, totalHeight, marginLeft, marginUp, rCrd.Direction, out r, out r2);
            if (xCrd != null && yCrd != null)
            {
                if (getXLow && getXHigh && getYLow && getYHigh)
                {
                    _limitRect = new RectangleMotion(Description);
                    _limitRect.Alpha = 60;
                    _limitRect.LineWidth = 1;
                    _limitRect.PaintColor = Color.DeepPink;
                    _limitRect.FillColor = Color.DeepPink;
                    _limitRect.IsFill = true;
                    _shapes.Add(_limitRect);
                    _limitRect.StartPoint = new PointF((float)x1, (float)y1);
                    _limitRect.EndPoint = new PointF((float)x2, (float)y2);
                }
            }
        }
    }
}
